home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / libs / unixlib.lha / unix / src / fnmatch.c < prev    next >
C/C++ Source or Header  |  1996-08-19  |  5KB  |  209 lines

  1. /* Copyright (C) 1991, 1992, 1993 Free Software Foundation, Inc.
  2.  
  3. NOTE: The canonical source of this file is maintained with the GNU C Library.
  4. Bugs can be reported to bug-glibc@prep.ai.mit.edu.
  5.  
  6. This program is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by the
  8. Free Software Foundation; either version 2, or (at your option) any
  9. later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #ifdef HAVE_CONFIG_H
  21. #include <config.h>
  22. #endif
  23.  
  24. #include <errno.h>
  25. #include <fnmatch.h>
  26.  
  27. #include <ctype.h>
  28.  
  29. #if defined (STDC_HEADERS) || !defined (isascii)
  30. #define ISASCII(c) 1
  31. #else
  32. #define ISASCII(c) isascii(c)
  33. #endif
  34.  
  35. #define ISUPPER(c) (ISASCII (c) && isupper (c))
  36.  
  37.  
  38. /* Comment out all this code if we are using the GNU C Library, and are not
  39.    actually compiling the library itself.  This code is part of the GNU C
  40.    Library, but also included in many other GNU distributions.  Compiling
  41.    and linking in this code is a waste when using the GNU C library
  42.    (especially if it is a shared library).  Rather than having every GNU
  43.    program understand `configure --with-gnu-libc' and omit the object files,
  44.    it is simpler to just do this in the source for each such file.  */
  45.  
  46. #if defined (_LIBC) || !defined (__GNU_LIBRARY__)
  47.  
  48.  
  49. #if !defined(__GNU_LIBRARY__) && !defined(STDC_HEADERS) && !defined(__SASC)
  50. extern int errno;
  51. #endif
  52.  
  53. /* Match STRING against the filename pattern PATTERN, returning zero if
  54.    it matches, nonzero if not.  */
  55. int
  56. fnmatch (const char *pattern, const char *string, int flags)
  57. {
  58.   register const char *p = pattern, *n = string;
  59.   register char c;
  60.  
  61. /* Note that this evalutes C many times.  */
  62. #define FOLD(c)    ((flags & FNM_CASEFOLD) && ISUPPER (c) ? tolower (c) : (c))
  63.  
  64.   while ((c = *p++) != '\0')
  65.     {
  66.       c = FOLD (c);
  67.  
  68.       switch (c)
  69.     {
  70.     case '?':
  71.       if (*n == '\0')
  72.         return FNM_NOMATCH;
  73.       else if ((flags & FNM_FILE_NAME) && *n == '/')
  74.         return FNM_NOMATCH;
  75.       else if ((flags & FNM_PERIOD) && *n == '.' &&
  76.            (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
  77.         return FNM_NOMATCH;
  78.       break;
  79.  
  80.     case '\\':
  81.       if (!(flags & FNM_NOESCAPE))
  82.         {
  83.           c = *p++;
  84.           c = FOLD (c);
  85.         }
  86.       if (FOLD (*n) != c)
  87.         return FNM_NOMATCH;
  88.       break;
  89.  
  90.     case '*':
  91.       if ((flags & FNM_PERIOD) && *n == '.' &&
  92.           (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
  93.         return FNM_NOMATCH;
  94.  
  95.       for (c = *p++; c == '?' || c == '*'; c = *p++, ++n)
  96.         if (((flags & FNM_FILE_NAME) && *n == '/') ||
  97.         (c == '?' && *n == '\0'))
  98.           return FNM_NOMATCH;
  99.  
  100.       if (c == '\0')
  101.         return 0;
  102.  
  103.       {
  104.         char c1 = (!(flags & FNM_NOESCAPE) && c == '\\') ? *p : c;
  105.         c1 = FOLD (c1);
  106.         for (--p; *n != '\0'; ++n)
  107.           if ((c == '[' || FOLD (*n) == c1) &&
  108.           fnmatch (p, n, flags & ~FNM_PERIOD) == 0)
  109.         return 0;
  110.         return FNM_NOMATCH;
  111.       }
  112.  
  113.     case '[':
  114.       {
  115.         /* Nonzero if the sense of the character class is inverted.  */
  116.         register int not;
  117.  
  118.         if (*n == '\0')
  119.           return FNM_NOMATCH;
  120.  
  121.         if ((flags & FNM_PERIOD) && *n == '.' &&
  122.         (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
  123.           return FNM_NOMATCH;
  124.  
  125.         not = (*p == '!' || *p == '^');
  126.         if (not)
  127.           ++p;
  128.  
  129.         c = *p++;
  130.         for (;;)
  131.           {
  132.         register char cstart = c, cend;
  133.  
  134.         if (!(flags & FNM_NOESCAPE) && c == '\\')
  135.           cstart = cend = *p++;
  136.  
  137.         cstart = cend = FOLD (cstart);
  138.  
  139.         if (c == '\0')
  140.           /* [ (unterminated) loses.  */
  141.           return FNM_NOMATCH;
  142.  
  143.         c = *p++;
  144.         c = FOLD (c);
  145.  
  146.         if ((flags & FNM_FILE_NAME) && c == '/')
  147.           /* [/] can never match.  */
  148.           return FNM_NOMATCH;
  149.  
  150.         if (c == '-' && *p != ']')
  151.           {
  152.             cend = *p++;
  153.             if (!(flags & FNM_NOESCAPE) && cend == '\\')
  154.               cend = *p++;
  155.             if (cend == '\0')
  156.               return FNM_NOMATCH;
  157.             cend = FOLD (cend);
  158.  
  159.             c = *p++;
  160.           }
  161.  
  162.         if (FOLD (*n) >= cstart && FOLD (*n) <= cend)
  163.           goto matched;
  164.  
  165.         if (c == ']')
  166.           break;
  167.           }
  168.         if (!not)
  169.           return FNM_NOMATCH;
  170.         break;
  171.  
  172.       matched:;
  173.         /* Skip the rest of the [...] that already matched.  */
  174.         while (c != ']')
  175.           {
  176.         if (c == '\0')
  177.           /* [... (unterminated) loses.  */
  178.           return FNM_NOMATCH;
  179.  
  180.         c = *p++;
  181.         if (!(flags & FNM_NOESCAPE) && c == '\\')
  182.           /* XXX 1003.2d11 is unclear if this is right.  */
  183.           ++p;
  184.           }
  185.         if (not)
  186.           return FNM_NOMATCH;
  187.       }
  188.       break;
  189.  
  190.     default:
  191.       if (c != FOLD (*n))
  192.         return FNM_NOMATCH;
  193.     }
  194.  
  195.       ++n;
  196.     }
  197.  
  198.   if (*n == '\0')
  199.     return 0;
  200.  
  201.   if ((flags & FNM_LEADING_DIR) && *n == '/')
  202.     /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz".  */
  203.     return 0;
  204.  
  205.   return FNM_NOMATCH;
  206. }
  207.  
  208. #endif    /* _LIBC or not __GNU_LIBRARY__.  */
  209.